<?php
    class Admin_home extends Admin_controller{
        function __construct(){
            parent :: __construct();
            $this->_data['sidebar'] = 'admin_home';
            $this->load->model("Media_m");
            $this->load->model("News_m");
        }
        
        function index(){
            $this->list_home_images();
        }
        
        function select_home_news(){
            $this->display_view("add_home_news");
        }
        
        function upload_images(){
        is_admin();
        if ($this->input->post('submit')) {
           
                $image_path = './assets/images/home';
                $thumb_path = $image_path . '/thumb';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' => TRUE
                );
               $this->load->library('upload',$config);
                $this->load->library('Multi_upload'); 
                $this->upload->initialize($config);
                
                $files = $this->multi_upload->go_upload();
                
                if (!$files) {
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                    redirect('admin_home/upload_images');
                } else {
                    $this->load->library('image_lib');
                    for ($i = 0; $i < count($files); $i++) {
                        $config = array(
                            'source_image' => $files[$i]['file'], //get original image
                            'new_image' => $thumb_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 120,
                            'height' => 80
                        );
                        $this->image_lib->initialize($config);  //load library
                        $this->image_lib->resize();

                        $config1 = array(
                            'source_image' => $files[$i]['file'], //get original image
                            'new_image' => $image_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 1024,
                            'height' => 768
                        );
                        $this->image_lib->initialize($config1);  //load library
                        $this->image_lib->resize();

                        $query = $this->Media_m->add_home_images(array('file_name' => $files[$i]['name']));
                    }

                    $data = array('upload_data' => $files);
                    if ($query) {
                        $this->session->set_flashdata('error', 'Upload'.' ' . lang('success'));
                    } else {
                        $this->session->set_flashdata('error', 'Upload'.' ' . lang('fail'));
                    }
                    redirect('admin_home/list_home_images');
                }
           
        } else {
            $this->display_admin('admin/home/upload_home_image');
        }
     
     }
     
     function list_home_images(){
         $this->_data['home_images'] = $this->Media_m->get_home_images();
         $this->display_admin('admin/home/list_home_images');
     }
     
     function delete_home_image($id) {
        is_admin();
        $media = $this->Media_m->get_detail_home_image($id);
       
        
            $file_name = './assets/images/home/' . $media['file_name'];
            $thumbs_name = './assets/images/home/thumb/' . $media['file_name'];
            if ($media['file_name'] != null) {
                delete_images($file_name, $thumbs_name);
           
        	}

        $query = $this->Media_m->delete_home_image($id);
        if ($query)
            $this->session->set_flashdata('error', 'Xóa ảnh thành công');
        else
            $this->session->set_flashdata('error', 'Xóa ảnh thất bại');
        redirect('admin_home/list_home_images');
    }
    
    function delete_multi_home_images() {
        is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            $media = $this->Media_m->get_detail_home_image($adelete[$i]);
           
               $file_name = './assets/images/home/' . $media['file_name'];
               $thumbs_name = './assets/images/home/thumb/' . $media['file_name'];
                if ($media['file_name'] != null) {
                    delete_images($file_name, $thumbs_name);
                }
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Media_m->delete_multi_home_images($list);
        if ($query)
            $this->session->set_flashdata('error', 'Xóa ảnh thành công');
        else
            $this->session->set_flashdata('error', 'Xóa ảnh thất bại');
        redirect('admin_home/list_home_images');
    }
    
    function home_news(){
    	if($this->input->post('submit')){
    		$news = $this->input->post("news_id");
    		$query = $this->News_m->add_home_news(array("news_id"=>$news, "date_posted"=>now()));	
    		  if ($query)
           			 $this->session->set_flashdata('error', 'Đăng tin tức lên trang chủ thành công');
        	  else
            		$this->session->set_flashdata('error', 'Đăng tin tức lên trang chủ thất bại');
        	  redirect('admin_home/home_news');
    	}else{
    		$this->display_admin('admin/home/add_home_news');	
    	}
    	
    }
 }
?>
